package com.atpy.reggie.controller;

import com.atpy.reggie.common.R;
import com.atpy.reggie.dto.SetmealDto;
import com.atpy.reggie.entity.Setmeal;
import com.atpy.reggie.entity.SetmealDish;
import com.atpy.reggie.service.CategoryService;
import com.atpy.reggie.service.SetmealDishService;
import com.atpy.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/*
逃禅管理
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealComtroller {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }
//    Request URL: http://localhost:8080/setmeal/page?page=1&pageSize=10

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
//        分页构造器
        Page<Setmeal> setmealPage=new Page<>(page,pageSize);
        Page<SetmealDto> DtoPage=new Page<>(page,pageSize);
//        条件构造器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null,Setmeal::getName,name);

        lambdaQueryWrapper.orderByDesc(Setmeal::getCreateTime);
        setmealService.page(setmealPage,lambdaQueryWrapper);
        BeanUtils.copyProperties(setmealPage,DtoPage,"records");
        final val records = setmealPage.getRecords();
        List<SetmealDto> list=records.stream().map((item)->{
            final val setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            final val categoryId = item.getCategoryId();
            final val category = categoryService.getById(categoryId);
            if (category!=null){
                final val categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        DtoPage.setRecords(list);
        log.info("分页信息{}",DtoPage);
        return R.success(DtoPage);

    }
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam("ids") List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }
    @GetMapping("/{id}")
    public R<SetmealDto> getsetmeal(@PathVariable("id") Long id){
        final val setmeal = setmealService.getById(id);
        final val setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        final val list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info("{}",setmealDto);
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }
//localhost:8080/setmeal/status/1?ids=1568121950753742850,1568129989196070914
    @PostMapping("/status/{statu}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updatestatus(@PathVariable("statu") int statu,@RequestParam("ids") List<Long> ids){
        final val setmeals = setmealService.listByIds(ids);
        final val list = setmeals.stream().map((item) -> {
            item.setStatus(statu);
            return item;
        }).collect(Collectors.toList());
        setmealService.updateBatchById(list);
        return R.success("状态修改成功");
    }
//setmeal/list?categoryId=1413386191767674881&status=1

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        final val setmeals = setmealService.list(setmealLambdaQueryWrapper);

        return R.success(setmeals);
    }




//    Request URL: http://localhost:8080/setmeal/dish/1568137980175691777

//    @GetMapping("/dish/{id}")
//    public R<SetmealDto> getSetmealdtail(@PathVariable("id") long id){
//
//        Setmeal setmeal = setmealService.getById(id);
//        SetmealDto setmealDto = new SetmealDto();
//        BeanUtils.copyProperties(setmeal,setmealDto);
//        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(SetmealDish::getSetmealId,id);
//        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
//        setmealDto.setSetmealDishes(setmealDishes);
//        return R.success(setmealDto);
//    }

    //    Request URL: http://localhost:8080/setmeal/dish/1568137980175691777

    @GetMapping("/dish/{id}")
    public R<Setmeal> getSetmealdtail(@PathVariable("id") long id){

        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        return R.success(setmealDto);
    }
}
